<?php
header("Content-Type: text/html;charset=utf-8");
include_once('config/config.php');
if ($_GET) {
  $getParams = $_GET;
  $sql = 'SELECT * FROM `department` WHERE `id` = ' . $getParams['id'] . '';
  $getData = dbFind($sql);
}
// pre($getData);


if ($_POST) {
  $params = $_POST;

  $action = $_POST['action'];
  if ($action == 'checkName') {
    $name = $_POST['name'];
    if (empty(trim($name))) {
      error('部门不能为空');
    }
    $id = $_POST['id'];
    $allName = dbAll("SELECT `name` FROM `department` WHERE `id` <> '$id '");
    foreach ($allName as $item) {
      if ($name == $item['name']) {
        error('部门已存在');
        break;
      }
    }
    success('部门可以使用');
  }

  //封装更新数组
  $data = [
    'name' => $params['name']
  ];

  $sqlWhere = "`id` = '" . $getParams['id'] . "'";
  $res = dbUpdate('department', $data, $sqlWhere);
  if ($res) {
    ShowMsg('更新成功', 'departmentList.php');
    exit;
  } else {
    ShowMsg('更新失败');
    exit;
  }
}

?>

<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="utf-8">
  <title>部门</title>
  <!-- 引用公共头部 -->
  <?php include_once('common/meta.php'); ?>

  <style>
    #avatar {
      width: 200px;
      user-select: none;
    }

    #img {
      width: 200px;
      display: block;
    }

    .icon-list {
      margin-right: 5px;
    }

    .well {
      padding-bottom: 0;
    }

    form {
      margin-bottom: 0px;
    }
  </style>
</head>

<body>

  <!-- 引用公共头部 -->
  <?php include_once('common/header.php'); ?>

  <!-- 引用侧边菜单 -->
  <?php include_once('common/sidebar.php'); ?>

  <div class="content">
    <div class="header">
      <h1 class="page-title">修改部门信息</h1>
    </div>
    <ul class="breadcrumb">
      <li><a href="index.php">首页</a> <span class="divider">/</span></li>
      <li class="active">部门</li>
    </ul>

    <div class="container-fluid">
      <div class="row-fluid">

        <div class="btn-toolbar">
          <button class="btn btn-primary " onClick="location='departmentList.php'"><i class="icon-list"></i>部门</button>
          <div class="btn-group">
          </div>
        </div>

        <div class="well">
          <div id="myTabContent" class="tab-content">
            <div class="tab-pane active in" id="home">
              <form method="POST" enctype="multipart/form-data">
                <?php
                $str = <<<LABEL
                <label>部门</label>
                <input type="text" value="{$getData['name']}" class="input-xxlarge" name="name" >
                <span id="name" style="margin-left: 10px;"></span>
LABEL;
                echo $str;
                ?>
                <br />
                <input style="margin-bottom: 10px;" class="btn btn-primary" type="submit" value="提交" />
              </form>
            </div>
          </div>
        </div>

        <!-- 引用公共底部 -->
        <?php include_once('common/footer.php'); ?>

      </div>
    </div>
  </div>
</body>

</html>
<!-- 引用script -->
<?php include_once('common/script.php'); ?>

<script>
  $('input[name="name"]').change(function() {
    let name = $(this).val();

    $.ajax({
      type: 'post',
      url: 'departmentEdit.php',
      data: {
        id: <?php echo $getParams['id']; ?>,
        name,
        action: 'checkName'
      },
      dataType: 'json',
      success: function(res) {
        if (res.code === 1) {
          $('#name').html(res.msg).css({
            "color": "green"
          })
        } else {
          $('#name').html(res.msg).css({
            "color": "red"
          })
        }
      },
      // 请求失败的回调函数 404 301 500等
      error: function(error) {
        console.log(error)
      }
    })
  });
</script>